/* 
 * File:   ListadoCampanias.cpp
 * Author: agu
 * 
 * Created on 22 de junio de 2014, 13:21
 */

#include "editor_ListadoCampanias.h"
#include <iostream>
#include <sstream>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <fstream>  
#include <cstdio>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::stringstream;

ListadoCampanias::ListadoCampanias() : m_VBox(Gtk::ORIENTATION_VERTICAL),
m_Button_Back("Volver") {
    this->loadWidgets();
    this->connectWidgets();
}

ListadoCampanias::~ListadoCampanias() {
}

void ListadoCampanias::loadWidgets() {
    set_title("Listado de campañas");
    set_border_width(5);
    set_default_size(400, 600);
    set_position(Gtk::WIN_POS_CENTER_ALWAYS);
    add(m_VBox);

    //Add the TreeView, inside a ScrolledWindow, with the button underneath:
    m_ScrolledWindow.add(m_TreeView);

    //Only show the scrollbars when they are necessary:
    m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

    m_VBox.pack_start(m_ScrolledWindow);
    m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);

    m_ButtonBox.pack_start(m_Button_Back, Gtk::PACK_SHRINK);
    m_ButtonBox.set_border_width(5);
    m_ButtonBox.set_layout(Gtk::BUTTONBOX_CENTER);
    //Create the Tree model:
    m_refTreeModel = Gtk::ListStore::create(m_Columns);
    m_TreeView.set_model(m_refTreeModel);
    addColumns();

}

void ListadoCampanias::connectWidgets() {

    m_Button_Back.signal_clicked().connect(sigc::mem_fun(*this,
            &ListadoCampanias::back));
    m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this,
            &ListadoCampanias::select));
    this->signal_hide().connect(sigc::mem_fun(this, &ListadoCampanias::close));
    this->signal_show().connect(sigc::mem_fun(this, &ListadoCampanias::loadRows));
    show_all_children();
}

void ListadoCampanias::loadRows() {
    //Fill the TreeView's model
    m_refTreeModel->clear();
    Gtk::TreeModel::Row row;
    this->loadCampaignsDirectory(row);
}

void ListadoCampanias::addColumns() {
    //Add the TreeView's view columns:
    //This number will be shown with the default numeric formatting.
    m_TreeView.append_column("Name", m_Columns.m_col_name);

}

void ListadoCampanias::setWindowsBack(Gtk::Window *windowBack) {
    this->windowsBack = windowBack;
}

void ListadoCampanias::run() {    
    this->show();
}

void ListadoCampanias::close() {
    this->hide();
}

void ListadoCampanias::back() {
    this->set_visible(false);
    this->windowsBack->set_visible(true);
}

void ListadoCampanias::select(const Gtk::TreeModel::Path& path,
        Gtk::TreeViewColumn* /* column */) {
    Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path);
    if (iter) {
        Gtk::TreeModel::Row row = *iter;
        Glib::ustring name = row[m_Columns.m_col_name];        
        string filePath("campaigns/");
        filePath.append(name);
        //std::cout << "File path=" << filePath << std::endl;   
        this->editCamp.setCampaignName(name);
        this->editCamp.run();
        this->loadRows();
    }
}

void ListadoCampanias::loadCampaignsDirectory(Gtk::TreeModel::Row row) {
    // variables
    string ruta = "campaigns/";
    DIR *dir = NULL;
    struct dirent *drnt = NULL;
    const char* puntos = ".";
    const char* puntos2 = "..";
    dir = opendir(ruta.c_str());
    if (dir) {
        while ((drnt = readdir(dir))) {
            if ((strcmp(drnt->d_name, puntos) == 0) ||
                    ((strcmp(drnt->d_name, puntos2) == 0))) continue;
            stringstream ss;
            ss << drnt->d_name;
            string aux(ss.str());
            row = *(m_refTreeModel->append());
            row[m_Columns.m_col_name] = aux;
        }
        closedir(dir);
    }
}